package com.FH.organization.manage_line;

import com.FH.organization.manage_node.ManageNodeResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/24.
 */
@Stateless
public class ManageLineDAOImpl extends AccessDB implements ManageLineDAO {

    @Inject
    ManageNodeResource manageNodeResource;
    /**
     * 查询线表信息
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getManageLineInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from manageline where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("line_id")) {
                sql_w += " AND line_id = '" + json.getString("line_id") + "'";
            }
            if (json.has("sourceId")) {
                if(json.has("org_id")){
                    sql_w += " AND (sourceId = '" + json.getString("sourceId") + "' or  sourceId =" + " '" + json.getString("org_id") + "')";
                }else{
                    sql_w += " AND sourceId = '" + json.getString("sourceId") + "'";
                }
            }

            if (json.has("manage_id")) {
                sql_w += " AND manage_id = '" + json.getString("manage_id") + "'";
            }
            if (json.has("type")) {
                sql_w += " AND type = '" + json.getString("type") + "'";
            }
            if (json.has("targetId")) {
                sql_w += " AND targetId = '" + json.getString("targetId") + "'";
            }
            if (json.has("source_key")) {
                sql_w += " AND source_key = '" + json.getString("source_key") + "'";
            }
            if (json.has("target_key")) {
                sql_w += " AND target_key = '" + json.getString("target_key") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<ManageLineShowDTO> result = new ArrayList<ManageLineShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select line_id,manage_id,sourceId,targetId,type,line_desc,source_key,target_key  from manageline  where 1=1 " + sql_w + " ORDER BY manage_id DESC " + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    ManageLineShowDTO manageLineShowDTO = new ManageLineShowDTO();
                    manageLineShowDTO.setLine_id(seresult.get(i)[1]);
                    manageLineShowDTO.setManage_id(seresult.get(i)[2]);
                    manageLineShowDTO.setSourceId(seresult.get(i)[3]);
                    manageLineShowDTO.setTargetId(seresult.get(i)[4]);
                    manageLineShowDTO.setType(seresult.get(i)[5]);
                    manageLineShowDTO.setLine_desc(seresult.get(i)[6]);
                    manageLineShowDTO.setSource_key(seresult.get(i)[7]);
                    manageLineShowDTO.setTarget_key(seresult.get(i)[8]);
                    if(null!=seresult.get(i)[7]&&!"".equals(seresult.get(i)[7])){
                        manageLineShowDTO.setSourceId_key(seresult.get(i)[3]+"_"+seresult.get(i)[7]);
                    }else {
                        manageLineShowDTO.setSourceId_key(seresult.get(i)[3]);
                    }
                    if(null!=seresult.get(i)[8]&&!"".equals(seresult.get(i)[8])){
                        manageLineShowDTO.setTargetId_key(seresult.get(i)[4]+"_"+seresult.get(i)[8]);
                    }else {
                        manageLineShowDTO.setTargetId_key(seresult.get(i)[4]);
                    }
                    ResultDTO nodelist=null,nodelistcurrent=null;
                    if(json.has("nodesdetails")){
                        nodelistcurrent = manageNodeResource.getManageNodeInfo("{nodeid:"+seresult.get(i)[3]+",manage_id:"+seresult.get(i)[2]+"}","1","20");
                        manageLineShowDTO.setNodelist_current(nodelistcurrent.getValues());

                        String nodeid = json.getString("nodesdetails");
                        nodelist = manageNodeResource.getManageNodeInfo("{nodeid:"+seresult.get(i)[4]+",manage_id:"+seresult.get(i)[2]+"}","1","20");
                        manageLineShowDTO.setNodelist(nodelist.getValues());
                    }

                    result.add(manageLineShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }


    /**
     * 添加节点信息
     * @param manageLinePullDTO
     * @return
     */
    @Override
    public ResultDTO addManageLineInfo(ManageLinePullDTO manageLinePullDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String QUERY = "";

        QUERY += "insert into manageline(line_id,manage_id,sourceId,targetId,type,line_desc,source_key,target_key) VALUES('" + uuid + "','" + manageLinePullDTO.getManage_id() + "'" +
                ",'" + manageLinePullDTO.getSourceId() + "','" + manageLinePullDTO.getTargetId() + "','" + manageLinePullDTO.getType() + "','" + manageLinePullDTO.getLine_desc() + "','"
                + manageLinePullDTO.getSource_key() + "','" + manageLinePullDTO.getTarget_key() + "')";
        //插入业务表


        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            System.out.print(QUERY);
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 删除线表信息
     * @param manageLinePullDTO
     * @return
     */
    @Override
    public ResultDTO deleteManageLineInfo(ManageLinePullDTO manageLinePullDTO) {
        //删除之前查看该表是否有记录
        String sql = "select count(1) from manageline where manage_id='"+manageLinePullDTO.getManage_id()+"'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if(!existName.equals("0")){

            String QUERY = "";

            QUERY += "delete from manageline where manage_id='"+manageLinePullDTO.getManage_id()+"'";

            result = this.addReportingTo(QUERY);
        }
        if(result!= 0){


            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);

        }
        return resultDTO;
    }

}
